package com.linln.admin.ad.controller;

import com.linln.admin.ad.validator.AdInfoValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.ad.domain.AdInfo;
import com.linln.modules.ad.service.AdInfoService;
import com.linln.modules.system.domain.Area;
import com.linln.modules.system.service.AreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hct
 * @date 2019/09/12
 */
@Controller
@RequestMapping("/ad/adInfo")
public class AdInfoController {

    @Autowired
    private AdInfoService adInfoService;
    @Autowired
    private AreaService areaService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("ad:adInfo:index")
    public String index(Model model, AdInfo adInfo) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();

        // 获取数据列表
        Example<AdInfo> example = Example.of(adInfo, matcher);
        Page<AdInfo> list = adInfoService.getPageList(example);
        for (AdInfo adinfo:list.getContent()
        ) {
            if(adinfo==null) continue;
            Area area = areaService.findByCode(adinfo.getArea());
            if(area != null){
                Area parea = areaService.getById(area.getPid());
                adinfo.setAreaname(area.getName());
                if(parea != null){
                    adinfo.setAreaname(parea.getName()+area.getName());
                }
            }
        }
        
        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/ad/adInfo/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("ad:adInfo:add")
    public String toAdd() {
        return "/ad/adInfo/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("ad:adInfo:edit")
    public String toEdit(@PathVariable("id") AdInfo adInfo, Model model) {
        model.addAttribute("adInfo", adInfo);
        return "/ad/adInfo/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"ad:adInfo:add", "ad:adInfo:edit"})
    @ResponseBody
    public ResultVo save(@Validated AdInfoValid valid, AdInfo adInfo) {
        // 复制保留无需修改的数据
        if (adInfo.getId() != null) {
            AdInfo beAdInfo = adInfoService.getById(adInfo.getId());
            EntityBeanUtil.copyProperties(beAdInfo, adInfo);
        }
        adInfo.setAdStatus(1);
        adInfo.setArea(0+"");
        // 保存数据
        adInfoService.save(adInfo);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("ad:adInfo:detail")
    public String toDetail(@PathVariable("id") AdInfo adInfo, Model model) {
        model.addAttribute("adInfo",adInfo);
        return "/ad/adInfo/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("ad:adInfo:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (adInfoService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}